//
//  MYGShareManagerController.swift
//  MYDoctor
//
//  Created by Denny on 2024/4/24.
//

import UIKit

class MYGShareManagerController: MYGBaseController {
var dataSource = [MYGShareMangerUserModel]()
    var fid = ""
    var farmerModel = MYCloudDiskContentModel()

    @IBOutlet weak var collectionViewHeight: NSLayoutConstraint!
    @IBOutlet weak var descLB: UILabel!
    @IBOutlet weak var collectionView: UICollectionView!
    override func viewDidLoad() {
        super.viewDidLoad()
        configDataSource()
        self.title = "共享"
        configCollectionView()
    }
    
    @IBAction func tapInvate(_ sender: UIButton) {
        MYGCloudDiskPopManager.showInviteSheet(self.view)
    }
    
    func configCollectionView() {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 12
        layout.minimumInteritemSpacing = 4
        let leftMargin = 12.0
        let midMargin = 18.0
        let itemWidth = (screenWidth - 2 * leftMargin - 4 * midMargin) / 5
        layout.itemSize = CGSize(width: itemWidth, height: 80)
        self.collectionView.register(cellWithClass: MYGShareManagerCell.self)
        self.collectionView.setCollectionViewLayout(layout, animated: true)
    }
    @IBAction func showDetail(_ sender: UIButton) {
        let vc = MYGCloudDiskEditBriefVC()
        vc.content = self.farmerModel.desc
        vc.farmerModel = self.farmerModel
        self.navigationController?.pushViewController(vc)
    }
    @IBAction func showAll(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected

        var row = 1
        if self.dataSource.count > 4 {
            if self.dataSource.count % 4 == 0 {
                row = self.dataSource.count / 4
            }else {
                row = (self.dataSource.count / 4) + 1
            }
        }
        
        if sender.isSelected == false {
            row = 1
        }
        let height = Double(row) * 88.0 + 42.0
        self.collectionViewHeight.constant = height
        
    }
    @IBAction func tapDelete(_ sender: UIButton) {
        MYGLoading.show()
        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .netdiskSharingExit(param: ["f_id":farmerModel.id])) { item in
            MYGLoading.hide()
            NotificationCenter.default.post(name: Notification.Name("MYCloudDiskViewController_configDataSource"), object: nil)
            if let second = self.navigationController?.viewControllers[1] {
                self.navigationController?.popToViewController(second, animated: true)
            }
        } failed: { _, error in
            MYGLoading.hide()
        }
    }
    
    func configDataSource() {
        MYGLoading.show()
        MYGNetwork<MYGMeAPI, MYGEmptyModel>.request(target: .netdiskSharingUser) { item in
            MYGLoading.hide()
        } failed: { _, error in
            MYGLoading.hide()
        }
        
        guard let user = MYGUserManager.getUser() else {return}
        var user1 = MYGShareMangerUserModel()
        user1.name = user.doctor.name
        user1.avatar = user.doctor.avatar
        dataSource.append(user1)
    
        self.collectionView.reloadData()
       
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.descLB.text = self.farmerModel.desc.count == 0 ? "暂无简介": self.farmerModel.desc
    }
}

extension MYGShareManagerController: UICollectionViewDelegate,UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYGShareManagerCell", for: indexPath) as! MYGShareManagerCell
        let model = dataSource[indexPath.row]
        cell.bindData(model)
        return cell
    }
}


